package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.TQuestion;
import com.qfedu.entity.User;
import com.qfedu.entity.Week;
import com.qfedu.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/OaNew")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 资源库
     * @return
     */
    @RequestMapping("/queryquestionbyno.do")
    public ResponseData getAllQuestion(int page, int limit){

        ResponseData responseData = new ResponseData();

        // 设置分页
        PageHelper.startPage(page,limit);

        List<TQuestion> questions = questionService.getAllQuestion();

        PageInfo<TQuestion> pageInfo = new PageInfo<>(questions);
        int total = (int) pageInfo.getTotal();

        responseData.setCount(total);
        responseData.setData(questions);

        return responseData;

    }

    /**
     * 修改资源库数据
     * @param tQuestion
     * @return
     */
    @RequestMapping("/questionupdate.do")
    public ResponseData updateQuestion(TQuestion tQuestion , HttpServletRequest request){

        ResponseData responseData = new ResponseData();


        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");

        String no = user.getNo();

        tQuestion.setU_no(no);

        int i = questionService.updateQuestion(tQuestion);
        if (i > 0){
            responseData.setCode(0);
            responseData.setMsg("修改成功");
        }else{

            responseData.setCode(1);
            responseData.setMsg("更新失败");
        }

        return responseData;
    }

    /**
     * 删除资源库数据
     * @param
     * @return
     */
    @RequestMapping("/questiondelete.do")
   public ResponseData deleteQuestion(int id,HttpServletRequest request){


       ResponseData responseData = new ResponseData();

       TQuestion tQuestion = new TQuestion();

       HttpSession session = request.getSession();

       User user = (User) session.getAttribute("user");

       String no = user.getNo();

       tQuestion.setU_no(no);

       int i = questionService.deleteQuestion(id);

       if (i > 0 ){

           responseData.setCode(0);
           responseData.setMsg("删除成功");
       }else{

           responseData.setCode(1);
           responseData.setMsg("删除失败");
       }

       return responseData;
   }
}
